/**
 * 
 */
package org.istt.web.action.file;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.istt.domain.AttachFileDetail;
import org.istt.domain.Category;
import org.istt.domain.File;
import org.istt.domain.User;
import org.istt.service.FileManager;
import org.istt.web.WebConstants;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Huy
 *
 */
public class FileReportAction extends ActionSupport {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3338690462530238561L;
	private String id;
	private FileManager manager;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Autowired
	public void setFileManager(FileManager manager) {
		this.manager = manager;
	}

	public FileBean getFileBean() {
		File f = manager.get(id);
		
		FileBean fb = new FileBean();
		fb.setFileId(f.getId());
		
		Category c = f.getCategory();
		
		fb.setCategory(c.getCategoryName());
		fb.setFieldName(c.getField().getName());
		fb.setSender(f.getSender());
		fb.setLocation(f.getLocation());
		fb.setContact(f.getContactName());
		fb.setContactPhone(f.getContactPhone());
		fb.setAnotherPaper(f.getOtherAttachFiles());
		fb.setRequiredTime(getText("report.requireTime") + c.getProcessTime());
		fb.setReturnDate(f.getOutputDate());
		fb.setNumber(f.getNumber());
		fb.setReceiver(((User)ActionContext.getContext().getSession().get(WebConstants.USER_INFO)).getFullname());
		
		List<FileDetail> fds = new ArrayList<FileReportAction.FileDetail>();
		int i = 0;
		for (AttachFileDetail fileDetail : f.getAttachFileDetails()) {
			FileDetail fd = new FileDetail();
			fd.setNo(++i);
			fd.setQuantity(fileDetail.getNumberOfcopy().intValue());
			fd.setFileName(fileDetail.getAttachFile().getAttachFileName());
			
			fd.setCopiedType(fileDetail.getDocumentType().getName());
			fds.add(fd);
		}
		fb.setFileDetails(fds);
		return fb;
	}
	
	public class FileBean {
		private String fileId;
		private String category;
		private String fieldName;
		private String sender;
		private String location;
		private String contact;
		private String contactPhone;
		private String anotherPaper;
		private String requiredTime;
		private List<FileDetail> fileDetails;
		private DateTime returnDate;
		private Integer number;
		private String receiver;
		
		public FileBean(){
			
		}
		
		public String getFileId() {
			return fileId;
		}
		public void setFileId(String fileId) {
			this.fileId = fileId;
		}
		public String getCategory() {
			return category;
		}
		public void setCategory(String category) {
			this.category = category;
		}
		public String getFieldName() {
			return fieldName;
		}
		public void setFieldName(String fieldName) {
			this.fieldName = fieldName;
		}
		public String getSender() {
			return sender;
		}
		public void setSender(String sender) {
			this.sender = sender;
		}
		public String getLocation() {
			return location;
		}
		public void setLocation(String location) {
			this.location = location;
		}
		public String getContact() {
			return contact;
		}
		public void setContact(String contact) {
			this.contact = contact;
		}
		public String getContactPhone() {
			return contactPhone;
		}
		public void setContactPhone(String contactPhone) {
			this.contactPhone = contactPhone;
		}
		public String getAnotherPaper() {
			return anotherPaper;
		}
		public void setAnotherPaper(String anotherPaper) {
			this.anotherPaper = anotherPaper;
		}
		public String getRequiredTime() {
			return requiredTime;
		}
		public void setRequiredTime(String requiredTime) {
			this.requiredTime = requiredTime;
		}
		public List<FileDetail> getFileDetails() {
			return fileDetails;
		}
		public void setFileDetails(List<FileDetail> fileDetails) {
			this.fileDetails = fileDetails;
		}
		public DateTime getReturnDate() {
			return returnDate;
		}
		public void setReturnDate(DateTime returnDate) {
			this.returnDate = returnDate;
		}
		
		public String getReturnedDate() {
			return returnDate.toString("dd/MM/yyyy");
		}
		
		public String getReturnedDayOfWeek() {
			return returnDate.dayOfWeek().getAsText(new Locale("vi"));
		}

		public Integer getNumber() {
			return number;
		}

		public void setNumber(Integer number) {
			this.number = number;
		}

		public String getReceiver() {
			return receiver;
		}

		public void setReceiver(String receiver) {
			this.receiver = receiver;
		}
	}
	
	public class FileDetail {
		private int no;
		private String fileName;
		private int quantity;
		private String copiedType;
		public int getNo() {
			return no;
		}
		public void setNo(int no) {
			this.no = no;
		}
		public String getFileName() {
			return fileName;
		}
		public void setFileName(String fileName) {
			this.fileName = fileName;
		}
		public int getQuantity() {
			return quantity;
		}
		public void setQuantity(int quantity) {
			this.quantity = quantity;
		}
		public String getCopiedType() {
			return copiedType;
		}
		public void setCopiedType(String copiedType) {
			this.copiedType = copiedType;
		}
	}
	
	public Map<String,String> getReportParameters() throws FileNotFoundException {
		String reportsDirPath = ServletActionContext.getServletContext().getRealPath("/jasper/");
		java.io.File reportsDir = new java.io.File(reportsDirPath);
		
		if (!reportsDir.exists()) {
		    throw new FileNotFoundException(String.valueOf(reportsDir));
		}
		
		Map<String,String> reportParameters=new HashMap<String,String>();
		reportParameters.put("reportsDir", reportsDir.getAbsolutePath());
		
		return reportParameters;
	}
}
